//char sensorArray[15];
//sensorArray[0] = 'V';

// this program switches between ADC pins on ATMega324
// Cassi Latino 2/27/2012

#define IR_Threshold 0x100
#define Ultra_Threshold 24
#define Current_Threshold 218 //NEEDS VALUE CHANGED TO PROPER VALUE AFTER JON DETERMINES IT

void init_adc(void) {
	ADCSRA |= (1 << ADPS2) | (1 << ADPS1)| (1 << ADPS0);   //bit to set for micro-controller for ADC at 156Khz
	ADMUX  |= 1 << REFS0 | 0 << REFS1;	//enable internal voltage reference
	ADCSRA |= 1 << ADEN;    //enable ADC
}

char read_sensor(){
	unsigned int adcValue;
	uint8_t theLow;
	uint8_t theHigh;
    char warning = 0;
    
    for(uint8_t i=3;i<=7;i++){
       
        ADMUX &= ~0x1F;          // clear shosen channel
        ADMUX |= i;             // select channel for conversion
		ADCSRA |= 1 << ADSC;	// establish single conversion
        
		while(ADCSRA & (1<<ADSC));
        theLow = ADCL;
		theHigh =ADCH;
        adcValue = ADCH<<8 | theLow;
        
        switch(i){
            case 3:
				//IR Left Sensor
                sensorArray[IrLeftH] = theHigh;
                sensorArray[IrLeftL] = theLow;
                if(adcValue >= IR_Threshold){warning += 8;}
                break;
            case 4:
				//IR Right Sensor
                sensorArray[IrRightH] = theHigh;
                sensorArray[IrRightL] = theLow;
                if(adcValue >= IR_Threshold){warning += 16;}
                break;
            case 5:
				//Ultra Sonic Sensor
                sensorArray[UltraH] = theHigh;
                sensorArray[UltraL] = theLow;
                if(adcValue <= Ultra_Threshold){warning += 32;}
                break;
            case 6:
				//Left Motor Current
                sensorArray[LeftAmpH] = theHigh;
                sensorArray[LeftAmpL] = theLow;
                if(adcValue >= Current_Threshold){warning += 64;}
                break;
            case 7:
				//Right Motor Current
                sensorArray[RightAmpH] = theHigh;
                sensorArray[RightAmpL] = theLow;
                if(adcValue >= Current_Threshold){warning += 128;}
                break;
            default:
                warning = 0;
                break;
        } //end switch statement for saving readings
    } //end for loop to select ADC channels and read
    return warning;
} // end char read_sensor()
